package com.yatube.yygh.hospital.controller.api;

import com.yatube.yygh.common.exception.ServiceRuntimeException;
import com.yatube.yygh.common.helper.HttpRequestHelper;
import com.yatube.yygh.common.response.R;
import com.yatube.yygh.common.response.RCodeEnum;
import com.yatube.yygh.common.util.MD5;
import com.yatube.yygh.hospital.service.HospitalService;
import com.yatube.yygh.hospital.service.HospitalSetService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @author Created with IDEA
 * @description -- 对外访问的控制层
 * @date 2024-04-23 23:05
 */
@Resource
@RequestMapping("api/hospital")
public class HospitalApiController {

    @Resource
    private HospitalService hospitalService;
    @Resource
    private HospitalSetService hospitalSetService;

    @PostMapping("save")
    public R save(HttpServletRequest request){
        // 获取传递过来的医院信息
        Map<String, String[]> reqMap = request.getParameterMap();

        //
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(reqMap);

        // 签名校验
        String sign = paramMap.get("sign").toString();
        String hoscode = paramMap.get("hoscode").toString();
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String md5SignKey = MD5.encrypt(signKey);

        if (!sign.equals(md5SignKey)){
            throw new ServiceRuntimeException(RCodeEnum.SIGN_ERROR);
        }

        hospitalService.save(paramMap);
        return R.ok();
    }
}
